﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Json;
using System.Collections.Generic;

namespace QFramework
{
    public static class HttpUtil
    {
        public static string Get(string url, Dictionary<string, string> headers = null)
        {
            HttpWebRequest httpWebRequest = WebRequest.CreateHttp(url);

            httpWebRequest.Method = "GET";

            if (headers != null)
            {
                foreach (var headerKV in headers)
                {
                    httpWebRequest.Headers.Add(headerKV.Key, headerKV.Value);
                }
            }

            var response = httpWebRequest.GetResponse() as HttpWebResponse;

            var stream = new StreamReader(response.GetResponseStream());

            var responseText = stream.ReadToEnd();
            stream.Close();
            response.Close();

            return responseText;
        }

        public static string Post(string url, string formContent, Dictionary<string, string> headers = null)
        {
            // http://liangxiegame.com/api-token-auth/
            HttpWebRequest httpWebRequest = WebRequest.CreateHttp(url);

            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Method = "POST";

            byte[] btBodys = Encoding.UTF8.GetBytes(formContent);
            httpWebRequest.ContentLength = btBodys.Length;
            httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

            if (headers != null)
            {
                foreach (var headerKV in headers)
                {
                    httpWebRequest.Headers.Add(headerKV.Key, headerKV.Value);
                }
            }

            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();

            httpWebResponse.Close();
            streamReader.Close();
            httpWebRequest.Abort();
            httpWebResponse.Close();

            return responseContent;
        }

        public static string Delete(string url, Dictionary<string, string> headers = null)
        {
            // http://liangxiegame.com/api-token-auth/
            HttpWebRequest httpWebRequest = WebRequest.CreateHttp(url);

            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.Method = "DELETE";

            if (headers != null)
            {
                foreach (var headerKV in headers)
                {
                    httpWebRequest.Headers.Add(headerKV.Key, headerKV.Value);
                }
            }

            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();

            httpWebResponse.Close();
            streamReader.Close();
            httpWebRequest.Abort();
            httpWebResponse.Close();

            return responseContent;
        }

        public static string GetToken(string username, string password)
        {
            var responseContent = Post("http://liangxiegame.com/api-token-auth/", string.Format("username={0}&password={1}", username, password));

            var responsJson = JsonValue.Parse(responseContent);

            return responsJson["token"];
        }
    }
}